package com.example.yk2021xm04.controller;


import com.example.yk2021xm04.model.RespBean;
import com.example.yk2021xm04.pojo.Todo;
import com.example.yk2021xm04.pojo.User;
import com.example.yk2021xm04.query.TodoQuery;
import com.example.yk2021xm04.service.ITodoService;
import io.swagger.models.auth.In;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZengQi
 * @since 2021-12-06
 */
@Controller
@RequestMapping("/todo")
public class TodoController {

    @Resource
    private ITodoService todoService;

    @RequestMapping("index")
    public String index() {
        return "todo/todo";
    }

    @RequestMapping("count")
    public String statistics() {
        return "todo/todo-statistics";
    }

    @RequestMapping("statistic")
    @ResponseBody
    public RespBean todoStatistic() {
        //创建时间分组 group by day
        List<Object> createTimeByDay = todoService.createTimeByDay();
        //一天中的创建待办事项数
        List<Object> createCountByDay = todoService.createCountByDay();
        //总待办事项数量
        Integer totalTodoCount = todoService.count();
        //今天的创建待办事项数量
        Integer TodoCountInDay = todoService.TodoCountInDay();

        Map<String, Object> res = new HashMap<>();
        res.put("num", createCountByDay);
        res.put("time", createTimeByDay);
        res.put("total", totalTodoCount);
        res.put("dayCount", TodoCountInDay);
        return RespBean.success("待办事项统计数据返回成功",res);
    }

    /**
     *
     * @author ZengQi
     * @date 2021/12/6 0006 10:45
     * @param todoQuery
     * @return Map<Object>
     * 查看自己创建的待办事项列表
     */
    @RequestMapping("list")
    @ResponseBody
    public Map<String, Object> todoList(TodoQuery todoQuery) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User user = (User) principal;
        return todoService.todoList(user.getId(), todoQuery);
    }

    /**
     * 添加更新待办事项页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("addOrUpdateTodoPage")
    public String addOrUpdatePage(Integer id, Model model) {
        if (null!=id) {
            Todo todo = todoService.getById(id);
            System.out.println(todo);
//            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//            String beginTime = dateTimeFormatter.format(todo.getBeginTime());
//            String endTime = dateTimeFormatter.format(todo.getEndTime());
//            model.addAttribute("beginTime",beginTime);
//            model.addAttribute("endTime",endTime);
            model.addAttribute("todo",todo);
        }
        return "todo/add_update";
    }

    @RequestMapping("delete")
    @ResponseBody
    public RespBean deleteTodo(Integer[] ids) {
        todoService.deleteTodo(ids);
        return RespBean.success("待办事项删除成功");
    }

    @RequestMapping("add")
    @ResponseBody
    public RespBean addTodo(Todo todo) {
        todoService.addTodo(todo);
        return RespBean.success("待办事项添加成功");
    }


    @RequestMapping("update")
    @ResponseBody
    public RespBean updateTodo(Todo todo) {
        todoService.updateTodo(todo);
        return RespBean.success("待办事项更新成功");
    }

    @RequestMapping("switch")
    @ResponseBody
    public RespBean switchStatus(Integer id) {
        todoService.switchStatus(id);
        return RespBean.success("待办事项状态更新成功");
    }
}
